package com.lagou.com.lagou.homework.Modules4.item1;

import java.io.*;
import java.util.ArrayList;
import java.util.Collection;

public class ReadWriteFile {

    public static void write(Object co) throws StudentAgeException, StudentIdException {
        ObjectOutputStream oos = null;
        //ObjectInputStream ois = null;
        try {
            oos = new ObjectOutputStream(new FileOutputStream("d:test.txt"));
            //ois = new ObjectInputStream(new FileInputStream("d:a.txt"));

            //Student st = new Student(01,"xl",12);

            //传入集合写入a.txt
            oos.writeObject(co);


            //System.out.println(" write pass");

            //Object obj = ois.readObject();
            //System.out.println("读到的内容是：" + obj);

            /*Collection c1 = new ArrayList();
            boolean b1 = c1.add(obj);
            System.out.println(c1);
            for (Object co: c1
                 ) {
                co.
            }*/

        } catch (IOException /*| ClassNotFoundException*/ e) {
            e.printStackTrace();
        } finally {
            if (null != oos){
                try {
                    oos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }

    }

    public static void read(Collection co) {

        ObjectInputStream ois = null;
        try {
            ois = new ObjectInputStream(new FileInputStream("d:test.txt"));


            Object obj = ois.readObject();
            //System.out.println("读到的内容是：" + obj);
            boolean b1 = co.add(obj);
            /*Collection c1 = new ArrayList();
            boolean b1 = c1.add(obj);
            System.out.println(c1);
            for (Object co: c1
                 ) {
                co.
            }*/

        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        } finally {

            if (null != ois){
                try {
                    ois.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }
}
